var express = require('express')
var app= express()
//加载body-parser模块
var bodyParser = require('body-parser')
//配置body-parser模块
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())
//配置mysql数据库
var mysql= require('mysql')
var connection= mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'list'
})
connection.connect()

//在服务器端，允许某些来源、某些接口、某些方法以某些形式被跨域调用。
// app.all('/', function(req, res, next) {
//     res.header("Access-Control-Allow-Origin", "*");
//     res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
//     res.header("Access-Control-Allow-Headers", "X-Requested-With");
//     res.header('Access-Control-Allow-Headers', 'Content-Type');
//     next();
// })
app.use(function(req,res,next){
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next()
})
//查询轮播图数据
app.get('/',function(req,res){ 
    //console.log("hha ")
    connection.query('SELECT * FROM `bannerlist`',function(err,data){
        if(err){
            console.log('获取失败')
        }else{
            
            // console.log(data)
            // connection.end()  
            res.status(200).json({
                success: true,
                message: 'ok',
                listdata: data
            })
        }
    })
})
app.get('/newslist',function(req,res){
  connection.query('SELECT * FROM `newsList`',function(err,data){
      if(err){
            console.log('获取失败')
      }else{
            //console.log(data)
          res.status(200).json({
              success: true,
              message: 'ok',
              newslist: data
          })
      }
  }) 
})
//通过id查询新闻模块
app.get('/news',function(req,res){
    var id =req.query.id
    //console.log(id)
    connection.query(`SELECT * FROM news WHERE id=${id}` ,function(err,data){
        // console.log(req.query)
        if(err){
              console.log('获取失败')
        }else{
              //console.log(data)
            res.status(200).json({
                success: true,
                message: 'ok',
                news: data
            })
        }
    }) 
  })

  //查询新闻评论页面

  app.get('/comment',function(req,res){
    var params =req.query
    var index =params.pageindex
    var name= 'newscomments'
    console.log(params.artid)
    if(params.artid>0 && params.artid< 100){
      name= 'newscomments'
    }else if(params.artid>100 && params.artid< 200){
       name= 'photoscomments'
    }
    // console.log(name)
        connection.query(`SELECT * FROM ${name} order by id desc LIMIT ${10*(index-1)},10` ,function(err,data){
            if(err){
                  console.log('获取失败')
            }else{
                  //console.log(data)
                res.status(200).json({
                    success: true,
                    message: 'ok',
                    newscomments: data
                })
            }
        }) 
    
    
  })

//   app.get('/newscomment',function(req,res){
//     var params =req.query
//     var index =params.pageindex
//     if(params.artid>0 && params.artid< 100){
//         connection.query(`SELECT * FROM newscomments order by id desc LIMIT ${10*(index-1)},10` ,function(err,data){
//             if(err){
//                   console.log('获取失败')
//             }else{
//                   //console.log(data)
//                 res.status(200).json({
//                     success: true,
//                     message: 'ok',
//                     newscomments: data
//                 })
//             }
//         }) 
//     }
    
//   })
//添加评论内容
app.post('/api/postcomment',function(req,res){
    var artid =req.query.artid
    // console.log(artid)
     console.log(req.body.content)
     if(artid>0 && artid< 100){
         connection.query(`INSERT INTO newscomments VALUES (null,'小蜜蜂',NOW(),'${req.body.content}')` ,function(err,data){
             if(err){
                  console.log('评论失败')
             }else{
               res.status(200).json({
                     success: true,
                     message: 'ok'
                })
             }
       }) 
     }
    
  })
//查询photo列表
app.get('/photoslist',function(req,res){
    connection.query('SELECT * FROM `photoslist`',function(err,data){
        if(err){
              console.log('获取失败')
        }else{
              //console.log(data)
            res.status(200).json({
                success: true,
                message: 'ok',
                photoslist: data
            })
        }
    }) 
  })
//查询图片开始

app.get('/getimages',function(req,res){
    console.log(req.query.id)
    var name= 'jiaju'
// if(req.query.id == '0'){
        switch(parseInt(req.query.id)){
            case 0 : name= 'jiaju';break;
            case 1 : name= 'jiaju';break;
            case 2 : name= 'sheying';break;
            case 3 : name= 'mingxing';break;
            case 4 : name= 'kongjian';break;
            case 5 : name= 'huxing';break;
        }
    // }
    console.log(name)
    connection.query(`SELECT * FROM ${name}`,function(err,data){
        if(err){
              console.log('获取失败')
        }else{
              console.log(data)
            res.status(200).json({
                success: true,
                message: 'ok',
                    img: data
            })
        }
    }) 
  })

  function getmore(name,callback){
    connection.query(`SELECT * FROM ${name}`,function(err,data){
        if(err){
            console.log('获取失败') 
        }else{
            // console.log(data)
            callback&&callback(data) 
        }
    })
  }
//查询图片结束
  //图片详情
  app.get('/api/getimageInfo',function(req,res){
    var id =req.query.imgid
    console.log(id)
    connection.query(`SELECT * FROM photosmore WHERE id=${id}` ,function(err,data){
        // console.log(req.query)
        if(err){
              console.log('获取失败')
        }else{
              //console.log(data)
            res.status(200).json({
                success: true,
                message: 'ok',
                photosm: data
            })
        }
    }) 
  })
//获取图片缩略图信息
app.get('/api/getthumimages',function(req,res){
    var id =req.query.imgid
    console.log(id)
    if(id>0 && id <100){

        connection.query(`SELECT * FROM suolvetu` ,function(err,data){
            // console.log(req.query)
            if(err){
                  console.log('获取失败')
            }else{
                  //console.log(data)
                res.status(200).json({
                    success: true,
                    message: 'ok',
                    suolvetudata: data
                })
            }
        }) 

    }
  })

  //获取商品展示数据
  app.get('/api/getgoods',function(req,res){
    var params =req.query
    var index =params.pageindex
        connection.query(`SELECT * FROM goodsshow LIMIT ${8*(index-1)},8` ,function(err,data){
            if(err){
                  console.log('获取失败')
            }else{
                  //console.log(data)
                res.status(200).json({
                    success: true,
                    message: 'ok',
                    goods: data
                })
            }
        })       
  })
//通过id查询商品的图片轮播图
app.get('/getGoodsById',function(req,res){
    var id= req.query.id
    connection.query('SELECT * FROM goodsimg'+id , function(err,data){
        if(err){
           return console.log('获取失败')
        }
        res.status(200).json({
            success: true,
            message: 'ok',
            goodsimg: data
        })
    })
})
//通过id获取商品更多信息
app.get('/getGoodsinfo',function(req,res){
    var id =req.query.id

        connection.query(`SELECT * FROM goodsshow WHERE id = ${id}` ,function(err,data){
            if(err){
                  console.log('获取失败')
            }else{
                  //console.log(data)
                res.status(200).json({
                    success: true,
                    message: 'ok',
                    goodsinfo: data
                })
            }
        })       
  })
//通过id,查询商品的详细信息（介绍）
app.get('/getGoodsDetaile',function(req,res){
    var id =req.query.id
// console.log(id)
        connection.query(`SELECT * FROM goodsdetail WHERE id = ${id}` ,function(err,data){
            if(err){
                  console.log('获取失败')
            }else{
                  //console.log(data)
                res.status(200).json({
                    status: 0,
                    message: data
                })
            }
        })       
  })
//通过id查询商品的评论
app.get('/getGoodsComments',function(req,res){
    var id =req.query.id
    var index =req.query.pageindex
// console.log(id)
        connection.query(`SELECT * FROM goodsshow${id} LIMIT ${10*(index-1)},10` ,function(err,data){
            if(err){
                  console.log('获取失败')
            }else{
                  //console.log(data)
                res.status(200).json({
                    status: 0,
                    message: data
                })
            }
        })       
  })
  //通过id查询productslist中的商品列表
  app.get('/api/goods/getshopcarlist',function(req,res){
    var id =req.query.id
// console.log(id)
        connection.query(`SELECT * FROM productslist WHERE id in (${id})` ,function(err,data){
            if(err){
                  console.log('获取失败')
            }else{
                  //console.log(data)
                res.status(200).json({
                    status: 0,
                    message: data
                })
            }
        })       
  })

app.listen(4000,function(){
    console.log('端口4000，loading...')
})